package com.lance.answer.gui.answer.gk.video.tmp.tmp;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.gk.answer.AnswerDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

// 运行完就删掉-----------
@Slf4j
public class DoAnswertmppppRepeat {

    public static void main(String[] args) throws Exception {


        // 晚点删掉-----------
        Thread.sleep(1500*60*60*1);

    ///    Thread.sleep(3*60*60*1500);
     //   Thread.sleep(3*60*60*500);

        int sync = 10;

       // String subjectName = "人力资源管理";
    //    String subjectName = "政治学原理";
      //  String subjectName = "计算机应用基础";

        List<String> subjectList = Lists.newArrayList();
       subjectList.add("毛泽东思想和中国特色社会主义理论体系概论"); //FIXME 毛泽东也有两套答题链接
        subjectList.add("形势与政策");
          subjectList.add("习近平新时代中国特色社会主义思想");
        subjectList.add("思想道德修养与法律基础"); // 上传文件的还要刷http://xinjiang.ouchn.cn/mod/assign/view.php?id=242355
         subjectList.add("计算机应用基础"); //
         subjectList.add("国家开放大学学习指南"); //

        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        // 获取所有的用户名和密码
        InputStream userInputStream = new FileInputStream("libs//allUser.txt");
        List<String> userString = IOUtils.readLines(userInputStream, "utf-8");

       for(String subjectName:subjectList){
         //  LinkedList<AnswerRequestDTO> requestUrl = Lists.newLinkedList();
           LinkedList<String> requestUrl = Lists.newLinkedList();
           log.info("subjectName:{}",subjectName);
           InputStream quizInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-quiz.section");
           List<String> list1 = IOUtils.readLines(quizInputStream,"UTF-8");

           for (String str : list1) {
               requestUrl.add(str.split(",")[0]);
           }
           InputStream answerInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");
           String answerStr = IOUtils.toString(answerInputStream,"UTF-8");
           List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
           Map<String, AnswerObject> answers = new HashMap<>();
           Map<String, AnswerObject> originAnswers = new HashMap<>();
           for (AnswerObject answerObject : list) {
               answers.put(answerObject.getQuestionCode(), answerObject);
               originAnswers.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()),answerObject);
           }

           InputStream inputStream2 = new FileInputStream("d:\\0104"+subjectName+".user");
           List<String> users = IOUtils.readLines(inputStream2, "utf-8");

           for(String user:users){
               String[] strs = user.split(",");
               String userCode = strs[0];
               String pwd = strs[1].substring(6, 14);
               pwd = "Ouchn" + pwd;
               AnswerDTO answerDTO = new AnswerDTO();
               answerDTO.setSubjectName(subjectName);
               answerDTO.setUsername(userCode);
               answerDTO.setPwd(pwd);
               answerDTO.setAnswers(answers);
               answerDTO.setOriginAnswers(originAnswers);
               answerDTO.setCommit(true);
               answerDTO.setPush(false);
               answerDTO.setAnswerUrl(requestUrl);
               answerQueue.add(answerDTO);
           }



       }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoAnswertmpppppTask(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }




}
